﻿using FactoryDeviceSystem.Models;
using FactoryDeviceSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryDeviceSystem
{
    public partial class DeviceSettingForm : Form
    {
        private bool isSaved = true;
        private DeviceInfo currentDev;
        private List<DeviceInfo> devices;
        private List<string> dataList = new List<string>();
        public DeviceSettingForm()
        {
            InitializeComponent();
        }

        private void DeviceSettingForm_Load(object sender, EventArgs e)
        {
            InitDeviceSet();
        }

        private void InitDeviceSet()
        {
            devices = ConfigUtil.LoadDevices();
            InitListBox();

            devNoTxt.TextChanged += Txt_TextChanged;
            devNameTxt.TextChanged += Txt_TextChanged;
            posTxt.TextChanged += Txt_TextChanged;
            addrTxt.TextChanged += Txt_TextChanged;
        }

        private void InitListBox()
        {
            dataList.Clear();
            foreach (DeviceInfo device in devices)
            {
                string item = $"{device.DeviceNo}\t{device.DeviceName}";
                dataList.Add(item);
            }
            listBox.DataSource = dataList;
        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string itemStr = listBox.SelectedItem.ToString();
            int devNo = int.Parse(itemStr.Split('\t').ToArray()[0]);
            currentDev = devices.Find(d => d.DeviceNo == devNo);
            if (currentDev == null)
                return;
            devNoTxt.Text = currentDev.DeviceNo.ToString();
            devNameTxt.Text = currentDev.DeviceName;
            posTxt.Text = currentDev.Position.ToString();
            addrTxt.Text = currentDev.StateAddress;

            isSaved = true;
        }

        private void confirmBtn_Click(object sender, EventArgs e)
        {
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.DeviceNo = int.Parse(devNoTxt.Text.Trim());
            deviceInfo.DeviceName = devNameTxt.Text.Trim();
            deviceInfo.Position = int.Parse(posTxt.Text.Trim());
            deviceInfo.StateAddress = addrTxt.Text.Trim();
            // 存在则修改
            DeviceInfo device = devices.Find(d => d.DeviceNo == deviceInfo.DeviceNo);
            if (device!=null)
            {
                device.DeviceName = deviceInfo.DeviceName;
                device.Position = deviceInfo.Position;
                device.StateAddress = deviceInfo.StateAddress;
            }
            else // 不存在则添加
            {
                devices.Add(deviceInfo);
            }

            InitListBox();
            SaveToFile();
            isSaved = true;
        }

        private void SaveToFile()
        {
            string filePath = Application.StartupPath + "\\ConfigFiles\\DeviceSet.json";
            string data = JsonConvert.SerializeObject(devices, Formatting.Indented);
            File.WriteAllText(filePath, data,Encoding.UTF8);
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            if (!isSaved)
            {
                DialogResult result = MessageBox.Show("当前修改并未保存，是否确认关闭?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }

        private void Txt_TextChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            devices.Remove(currentDev);
            currentDev = null;
            InitDeviceSet();
            SaveToFile();
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            devNoTxt.Text = "";
            devNameTxt.Text = "";
            posTxt.Text = "";
            addrTxt.Text = "";
        }
    }
}
